from django.shortcuts import render

# Create your views here.
from django.core.paginator import Paginator
from .ser import *
from rest_framework.views import APIView
from rest_framework.response import Response
class ClassifyAPIView(APIView):
    def get(self,request):
        data = Classify.objects.all()
        ser = ClassifySer(data,many=True)
        return Response(ser.data)
    def post(self,request):
        data = request.data
        ser = ClassifySer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class BooksAPIVIew(APIView):
    def get(self, request):
        sy = Books.objects.all()
        cpage = request.GET.get('p')
        p = Paginator(sy, 2)
        re = p.page(cpage)
        ser = BooksSer(re, many=True)
        return Response({'code': 200, 'list': ser.data, "tpage": p.num_pages})
    def post(self,request):
        data = request.data
        ser = BooksSer(data=data)
        ser.is_valid(raise_exception=True)
        ser.save()
        return Response(ser.data)
class Sousuo(APIView):
    def get(self,request):
        data = request.GET.get('data')
        aa = Books.objects.filter(name__contains=data)
        ser = BooksSer(aa,many=True)
        return Response(ser.data)